/**
 * Created by xuweijie on 17-5-9.
 */
//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function(){
    var element = layui.element();
});

//layer弹出层
layui.use('layer',function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active={
        notice: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/delrole.action",
                        type: 'post',
                        data: {
                            roleid: othis.roleid
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data === "删除成功！") {
                                window.location.href = "/admin/role/showdetail";
                                layer.msg(data);
                            }else{
                                layer.msg(data);
                            }

                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        //更新
        update: function (othis) {
            var content = $('#updatemodel');
            layer.open({
                type: 1,
                title:'修改角色',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '650px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: true, //开启遮罩关闭
                content: content,
                success: function (layero) {
                    // console.log(othis);
                    var roleInfoParent = $(othis).parents('tr');
                    var roleInfoItem = roleInfoParent.children("td");
                    $('#ckrolename').val(roleInfoItem[1].innerHTML);
                    $('#ckroledesc').val(roleInfoItem[2].innerHTML);

                    var roots = [];
                    var rootsTd =  roleInfoItem.children('button');
                    $(rootsTd).each(function () {
                        roots.push(this.innerText);
                    });
                    var checkboxs = $("#divroot").children('div');
                    $(checkboxs).each(function () {
                        // console.log($(this).children('span')[0].innerText);
                        if (roots.indexOf($(this).children('span')[0].innerText)!==-1){
                            $(this).addClass('layui-form-checked');
                        }else {
                            $(this).removeClass('layui-form-checked');
                        }
                    });
                    $("input:checkbox[name=ckroot]").each(function () {
                        if (roots.indexOf($(this).attr('title'))!==-1){
                            $(this).attr('checked',true);
                        }else {
                            $(this).attr('checked',false);
                        }
                    });
                    $('#roleid').val(othis.roleid);
                },
                yes: function (index, layero) {
                    var rootsBox = [];
                    $("input:checkbox[name=ckroot]:checked").each(function(){
                        rootsBox.push($(this).val());
                    });
                    if(rootsBox.length===0){
                        layer.msg("未选择权限");
                    }else {
                        //先序列化所有参数，再通过ajax提交数据
                        var params = $.param({
                            'rolename': $('#ckrolename').val(),
                            'roots': rootsBox,
                            'roledesc': $('#ckroledesc').val(),
                            'roleid':othis.roleid
                        }, true);
                        $.ajax({
                            url: '/updaterole.action',
                            type: 'post',
                            data: params,
                            dataType: 'json',
                            success: function (data) {
                                if (data === "更新成功") {
                                    window.location.href = "/admin/role/showdetail";
                                }
                                layer.msg(data);
                            }
                        });
                    }
                }
            });
        }
    };
    $('.delrole').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.roleid = $(this).attr('roleid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updaterole').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.roleid = $(this).attr('roleid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

/**
 * 添加角色
 * */
layui.use('form', function(){
    var form = layui.form();

    //监听提交
    form.on('submit(btaddrole)', function(data){
        var form = layui.form();
        //监听提交
        var rootsBox = [];
        $("input:checkbox[name=root]:checked").each(function(){
            rootsBox.push($(this).val());
        });
        if(rootsBox.length===0){
            layer.msg("未选择权限");
        }else{
            //先序列化所有参数，再通过ajax提交数据
            var params = $.param({
                'rolename': data.field.rolename,
                'roots': rootsBox,
                'roledesc': data.field.roledesc
            },true);
            $.ajax({
                url: '/addrole.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (data) {
                    // if (data === "添加成功！") {
                        window.location.href = "/admin/role/showdetail";
                    // }
                    layer.msg(data);
                }
            });
        }
        return false;
    });
});